define(function(require, exports, module) {
	var $ = require('$');
	var formcc = require('form');
	var nav = require("bootstrap_nav");
	var bootbox = require("bootbox");
	var store = require("store");
	var loader = require("./loader/loader");
	require("bootstrap-datetimepicker.min.css");
	//require("bootstrap_datetimepicker.zh-CN");
	require("bootstrap_datetimepicker");
	require('select2.css');
	require('select2');

	exports.init = function(divId) {
		new formcc({
			attrs : {
				title : "新建数据字典",
				spansize : 'span12',
				id : "dictionaryform",
				items : [
				         	{
					          isText : true,
					          label : "类型",
					          id : 'type',
					          name : 'name',
					          type : "text",
					          style : "width:50%",
					          placeholder : "请输入类型",
					          readonly : false,
					          validations :
					          {
					              required : true,
					              minlength : 2
					          }
					        },{
					          isText : true,
					          label : "主键",
					          id : 'data_key',
					          name : 'data_key',
					          type : "text",
					          style : "width:50%",
					          placeholder : "请输入主键"
					        },{
					          isText : true,
					          label : "键值",
					          id : 'data_value',
					          name : 'data_value',
					          type : "text",
					          style : "width:50%",
					          placeholder : "请输入键值"
					        },{
					          isText : true,
					          label : "备注",
					          id : 'comments',
					          name : 'comments',
					          type : "text",
					          style : "width:50%"
					        }
					    ],
				buttons : [ {
					text : "保存",
					id : "btn_save",
					type : "submit",
					style : "btn btn-contrast",
					backgroud : "btn-contrast"
				}, {
					text : "取消",
					id : "btn_cancel",
					style : "btn",
					type : "button"
				} ]

			},
			renderTo : divId,
			afterRender : function() {
				var theme = require('bootstrap_theme');
				theme.setValidateForm($("#dictionaryform"), saveAction);

				$('#btn_cancel').on('click', function() {
					nav.toPageView("admin/dictionary.html");
				});
			}
		});
	};
	
	//保存
	var saveAction = function() {
			var formdata = {
				type : $("#type").val(),
			    url : $("#url").val(),
			    data_key : $("#data_key").val(),
			    data_value : $("#data_value").val(),
			    comments : $("#comments").val()
			};
			loader.loading("正在提交，请稍等……");
			$.ajax({
				url : "/dictionary/doInsert.do",
				dataType : "json",
				data : "data=" + JSON.stringify(formdata),
				type : "POST",
				success : function(json) {
					if(json.flag == '000000'){
	          nav.toPageView("admin/dictionary.html");
	          loader.noloading();                   
          } else {
            oauth2Relogin();                    
          }
				},
				error : function(s) {
					loader.noloading();
					bootbox.alertTimeout("网络请求失败，请重试！");
				}
			});

	};
});
